package kr.co.handysoft.ft.policelogchecker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ConnManager implements ActionListener{
	
	private static Connection conn = null;
	JTextField url;
	JTextField user;
	JTextField pwd;
	JTextField db;
	JRadioButton b1;
	JRadioButton b2;
	JRadioButton b3;
	JRadioButton b4;
	JRadioButton b5;

	public ConnManager(JRadioButton b1, JRadioButton b2, JRadioButton b3, JRadioButton b4, JRadioButton b5,
			JTextField t1, JTextField t2, JTextField t3, JTextField t4)
	{
		this.b1 = b1;
		this.b2 = b2;
		this.b3 = b3;
		this.b4 = b4;
		this.b5 = b5;
		url = t1;
		user = t2;
		pwd = t3;
		db = t4;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String queueDB = null;
		try{
			if(b1.isSelected()){//oracle
				Class.forName(FTK.getProperty("ORC_DRIVER"));
				queueDB = FTK.getProperty("ORACLE");
				if (queueDB != null) {
					conn = DriverManager.getConnection(queueDB);
					conn.setAutoCommit(true);
				}
			}
			else if(b2.isSelected()){ //MySQL
				Class.forName(FTK.getProperty("MY_DRIVER"));
				queueDB = FTK.getProperty("MYSQL");
				if (queueDB != null) {
					conn = DriverManager.getConnection(queueDB);
					conn.setAutoCommit(true);
				}
			}
			else if(b3.isSelected()){ //SQL_SERVER
				Class.forName(FTK.getProperty("SS_DRIVER"));
				queueDB = FTK.getProperty("SQL_SERVER");
				if (queueDB != null) {
					conn = DriverManager.getConnection(queueDB);
					conn.setAutoCommit(true);
				}
			}
			else if(b4.isSelected()){ //SQLite
				Class.forName(FTK.getProperty("ORC_DRIVER"));
				queueDB = FTK.getProperty("ORACLE");
				if (queueDB != null) {
					conn = DriverManager.getConnection(queueDB);
					conn.setAutoCommit(true);
				}
			}
			else if(b5.isSelected()){ //Maria
				Class.forName(FTK.getProperty("Mria_DRIVER"));
				queueDB = FTK.getProperty("MariaDB").trim() + url + FTK.getProperty("MDB_PORT").trim() + db;
				if (queueDB != null) {
					conn = DriverManager.getConnection(queueDB);
					conn.setAutoCommit(true);
				}
			}
		} catch (SQLException se) {
		    System.out.println("Connection impossible.");
		    se.printStackTrace();
		    System.exit(1);
		  } catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	private Connection returnConn (){
		return conn;
	}
}
